home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5981 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.3 KB  |  87 lines

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Functions as parameters
  5. Date: Wed, 21 Feb 96 23:50:39 GMT
  6. Organization: none
  7. Message-ID: <824946639snz@genesis.demon.co.uk>
  8. References: <Dn4x09.8Hx@undergrad.math.uwaterloo.ca>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <Dn4x09.8Hx@undergrad.math.uwaterloo.ca>
  15.            clgonsal@undergrad.math.uwaterloo.ca
  16.            "Carl Laurence Gonsalves" writes:
  17.  
  18. >
  19. >I've often seen and even written code that uses function pointers like
  20. >this:
  21. >
  22. >
  23. >/* cut here */
  24. >#include <stdio.h>
  25. >
  26. >void foo( int (*func)( int ) ){
  27. >  printf("%d\n", func( 5 ) );
  28. >}
  29. >
  30. >int bar( int x ){
  31. >  return x*2;
  32. >}
  33. >
  34. >int
  35. >main(){
  36. >  foo( bar );
  37. >  return 0;
  38. >}
  39. >/* cut here */
  40. >
  41. >Recently though, I saw a similar piece of code that did something like
  42. >this:
  43. >
  44. >/* cut here */
  45. >void foo( int func( int ) ){
  46. >  printf("%d\n", func( 5 ) );
  47. >}
  48. >/* cut here */
  49. >
  50. >I tried compiling this with both Watcom C 10.0 and GNU C 2.6.3, and it
  51. >worked. So my question is: is this standard, or is this some weird compiler
  52. >extension.
  53.  
  54. It is standard.
  55.  
  56. >I've never seen this syntax before. Does it mean the same thing
  57. >as the first piece of code?
  58.  
  59. Yes. This is one half of what is known as the rewrite rule for function
  60. parameters. In the standard (section 6.7.1) it says:
  61.  
  62. "A declaration of a parameter as ``array of type'' shall be ajusted to
  63. ``pointer to type'', and a declaration of a paremeter as ``function
  64. returning type'' shall be adjusted to ``pointer to function returning type'' "
  65.  
  66. So the compiler treats:
  67.  
  68. void bar(int a[], void b(void)) {}
  69.  
  70. as if it were written as:
  71.  
  72. void bar(int *a, void (*b)(void)) {}
  73.  
  74. >If they are identical, why is the (*func)(int) syntax so much more common?
  75.  
  76. 1. It was the only way to do it in most pre-ANSI compilers so it is slightly
  77.    more portable. Also, old habits die hard.
  78.  
  79. 2. It makes it clear to the reader that a function pointer is being used
  80.    rather than a direct function call.
  81.  
  82. -- 
  83. -----------------------------------------
  84. Lawrence Kirby | fred@genesis.demon.co.uk
  85. Wilts, England | 70734.126@compuserve.com
  86. -----------------------------------------
  87.